package com.zhk.ScriptManagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhk.ScriptManagement.entity.Order;
import com.zhk.ScriptManagement.entity.vo.OrderVo;
import com.zhk.ScriptManagement.entity.vo.ScriptVo;
import com.zhk.ScriptManagement.mapper.OrderMapper;
import com.zhk.ScriptManagement.mapper.ScriptMapper;
import com.zhk.ScriptManagement.service.OrderService;
import com.zhk.ScriptManagement.util.CodeMsg;
import com.zhk.ScriptManagement.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Optional;

/**
 * @Author: zhouhongkun
 * @Description: 订单service实现类
 * @Date: 2021/4/5 21:03
 */
@Service
public class OrderServiceImpl implements OrderService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ScriptMapper scriptMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public Result addOrder(Order order) {
        try{
            //添加订单
            orderMapper.addOrder(order);
            ScriptVo script = scriptMapper.getScriptByScriptId(order.getScriptId());
            script.setCurrentNum(script.getCurrentNum()+order.getNum());
            if(script.getCurrentState() == 0){
                script.setCurrentState(1);
            }
            scriptMapper.updateCurrentScript(script);
            return Result.success("添加成功");
        }catch (Exception e){
            log.error(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus();
            return Result.error(CodeMsg.SERVER_EXCEPTION,"添加失败");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public Result deleteOrder(Order order) {
        try{
            orderMapper.deleteOrder(order.getOrderId());
            ScriptVo script = scriptMapper.getScriptByScriptId(order.getScriptId());
            int num = script.getCurrentNum() - order.getNum();
            script.setCurrentNum(num);
            if(num == 0){
                script.setCurrentState(0);
            }
            scriptMapper.updateCurrentScript(script);
            return Result.success("删除成功");
        }catch (Exception e){
            log.error(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus();
            return Result.error(CodeMsg.SERVER_EXCEPTION,"删除失败");
        }
    }

    @Override
    public Result getOrder(String size, String num) {
        try{
            Integer pageSize = null;
            Integer pageNum = null;
            if(Optional.ofNullable(size).isPresent() && Optional.ofNullable(num).isPresent()){
                pageNum = Integer.parseInt(num);
                pageSize = Integer.parseInt(size);
            }
            PageHelper.startPage(pageNum,pageSize);
            List<OrderVo> orderList = orderMapper.getOrder();
            PageInfo<OrderVo> pageInfo = new PageInfo<>(orderList);
            return Result.success(pageInfo);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.SERVER_EXCEPTION,"查询失败");
        }
    }

    @Override
    public Result getOrderByOrderId(Integer orderId) {
        try{
            OrderVo order = orderMapper.getOrderByOrderId(orderId);
            return Result.success(order);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.SERVER_EXCEPTION,"查询失败");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public Result updateOrder(Order order) {
        try{
            orderMapper.updateOrder(order);
            ScriptVo script = scriptMapper.getScriptByScriptId(order.getScriptId());
            if(order.getState() == 1){
                orderMapper.addHistoryScript(order);
                if(script.getCurrentState() == 1){
                    ScriptVo scriptVo = new ScriptVo();
                    scriptVo.setCurrentState(0);
                    scriptVo.setCurrentNum(0);
                    scriptMapper.updateCurrentScript(scriptVo);
                }
            }
            return Result.success("更新成功");
        }catch (Exception e){
            log.error(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus();
            return Result.error(CodeMsg.SERVER_EXCEPTION,"修改失败");
        }
    }
}
